//
//  NSString+Password.m
//  03.数据加密
//
//  Created by 刘凡 on 13-12-10.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "NSString+Password.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>
#import "gtmbase64/GTMBase64.h"
#import "NSData+AES.h"
#import "NSData+Base64.h"
@implementation NSString (Password)

#pragma mark 使用MD5加密字符串
- (NSString *)MD5
{
    const char *cStr = [self UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];

    CC_MD5(cStr, (int)strlen(cStr), digest);

    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02x", digest[i]];
    }

    return result;
}

#pragma mark 使用SHA1加密字符串
- (NSString *)SHA1
{
    const char *cStr = [self UTF8String];
    NSData *data = [NSData dataWithBytes:cStr length:self.length];
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    CC_SHA1(data.bytes, (int)data.length, digest);

    NSMutableString *result = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02x", digest[i]];
    }
    
    return result;
}
+(NSString *)encryptUseDES:(NSString*)plainText key:(NSString*)key
{
        NSString *ciphertext =nil;
        const char *textBytes = [plainText UTF8String];
        NSUInteger dataLength = [plainText length];
        unsigned char buffer[1024];
        memset(buffer, 0, sizeof(char));
//        Byte iv[] = {1,2,3,4,5,6,7,8};
        size_t numBytesEncrypted = 0;
        CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                                          kCCOptionPKCS7Padding|kCCOptionECBMode,
                                                          [key UTF8String],
                                                           kCCKeySizeDES,
                                                            NULL,
                                                            textBytes,
                                                            dataLength,
                                                            buffer,
                                                            1024,
                                                            &numBytesEncrypted);
        if (cryptStatus == kCCSuccess) {
                NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
                
            ciphertext = [[NSString alloc]initWithData:[GTMBase64 encodeData:data] encoding:NSUTF8StringEncoding];
            }
        return [ciphertext stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"="] ];
}
+(NSString *)encryptUseAES:(NSString *)plainText key:(NSString *)key
{
    
    NSData *cipherData=[[plainText dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key?key:DTCloudKitAppKey];
    NSString* base64Text = [cipherData base64EncodedString];
    
    return base64Text;
    
    
}
@end
